﻿using System;

namespace UniRx.Operators
{
	// Token: 0x0200023F RID: 575
	internal class AggregateObservable<TSource, TAccumulate> : OperatorObservableBase<TAccumulate>
	{
		// Token: 0x06000D62 RID: 3426 RVA: 0x0003FB34 File Offset: 0x0003DF34
		public AggregateObservable(IObservable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator) : base(source.IsRequiredSubscribeOnCurrentThread<TSource>())
		{
			this.source = source;
			this.seed = seed;
			this.accumulator = accumulator;
		}

		// Token: 0x06000D63 RID: 3427 RVA: 0x0003FB57 File Offset: 0x0003DF57
		protected override IDisposable SubscribeCore(IObserver<TAccumulate> observer, IDisposable cancel)
		{
			return null;
			//return this.source.Subscribe(new AggregateObservable<TSource, TAccumulate>.Aggregate(this, observer, cancel));
		}

		// Token: 0x0400078B RID: 1931
		private readonly IObservable<TSource> source;

		// Token: 0x0400078C RID: 1932
		private readonly TAccumulate seed;

		// Token: 0x0400078D RID: 1933
		private readonly Func<TAccumulate, TSource, TAccumulate> accumulator;

		// Token: 0x02000240 RID: 576
		private class Aggregate : OperatorObserverBase<TSource, TAccumulate>
		{
			// Token: 0x06000D64 RID: 3428 RVA: 0x0003FB6C File Offset: 0x0003DF6C
			public Aggregate(AggregateObservable<TSource, TAccumulate> parent, IObserver<TAccumulate> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
				this.accumulation = parent.seed;
			}

			// Token: 0x06000D65 RID: 3429 RVA: 0x0003FB8C File Offset: 0x0003DF8C
			public override void OnNext(TSource value)
			{
				try
				{
					this.accumulation = this.parent.accumulator(this.accumulation, value);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x06000D66 RID: 3430 RVA: 0x0003FBF8 File Offset: 0x0003DFF8
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000D67 RID: 3431 RVA: 0x0003FC30 File Offset: 0x0003E030
			public override void OnCompleted()
			{
				this.observer.OnNext(this.accumulation);
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0400078E RID: 1934
			private readonly AggregateObservable<TSource, TAccumulate> parent;

			// Token: 0x0400078F RID: 1935
			private TAccumulate accumulation;
		}
	}
}
